<?php
include(__DIR__ . '/config.php');

use PhpAmqpLib\Connection\AMQPStreamConnection;

$exchange_name = 'topic_logs';
$queue_name = 'topic_logs';

$connection = new AMQPStreamConnection(HOST,PORT,USER,PASS,VHOST);

$channel = $connection->channel();

$channel->exchange_declare($exchange_name, 'topic', false, false, false);

$channel->queue_declare($queue_name,false,true,false,false);

$binding_keys = array_slice($argv, 1);

if (empty($binding_keys)) {
    echo  "Usage: $argv[0] [binding_key]\n";
    exit(1);
}


foreach ($binding_keys as $binding_key) {
    $channel->queue_bind($queue_name, $exchange_name, $binding_key);
}

echo ' [*] Waiting for logs. To exit press CTRL+C', "\n";

$callback = function ($msg) {
    echo ' [x] ', $msg->delivery_info['routing_key'], ':', $msg->body, "\n";
};

$channel->basic_consume($queue_name, '', false, true, false, false, $callback);

while (count($channel->callbacks)) {
    $channel->wait();
}

$channel->close();
$connection->close();

